<template>
  <qx-table :dbRowClick="rowDblClick" :option="tableOption" :remoteData="remoteData">
    <template v-slot:top>
      <qx-form v-model="searchForm" :option="tableOption" isSearch @submit="startSearch"></qx-form>
    </template>
    <template v-slot:menuLeftR>
      <q-icon color="warning" name="mdi-map-marker-question cursor-pointer cursor-hover" size="1.5rem">
        <q-tooltip class="q-pa-md" v-bind="$tooltipAttr">{{ $t('Misc.DblClickTips') }}</q-tooltip>
      </q-icon>
    </template>
  </qx-table>
  <q-modal v-model="modalVisible" :header="`${$t('Misc.SpecifyDetails')}`" :menuBtn="false" :persistent="false"
           width="620">
    <QInfo :columns="tableOption.column || []" :rowInfo="formInfo" cols="2" headerWidth="120"></QInfo>
  </q-modal>
</template>
<script setup>
import { i18n, toI18nName } from 'boot/i18n.js'
import { getRemoteData } from 'src/enum/ajaxRemoteData.js'
import { computed, onMounted, ref } from 'vue'

const formInfo = ref({})
const AGVBrands = ref([])
const searchForm = ref({})
const modalVisible = ref(false)
const remoteData = ref({ dicUrl: '/tpm/yfagv/api/agvStatisticsChargeDetails/list' })
getRemoteData('agv_brand', '/yfagv/api/agvCfgBrand/list').then(data => (AGVBrands.value = data))
const endChargeReasons = computed(() => {
  return [{
    label: `${i18n.t('AGVCore.ChargeEndReasonTip1')}`, value: '0'
  }, {
    label: `${i18n.t('AGVCore.ChargeEndReasonTip2')}`, value: '1'
  }, {
    label: `${i18n.t('AGVCore.ChargeEndReasonTip3')}`, value: '2'
  }, {
    label: `${i18n.t('AGVCore.ChargeEndReasonTip4')}`, value: '3'
  }, {
    label: `${i18n.t('AGVCore.ChargeEndReasonTip5')}`, value: '4'
  }]
})
const tableOption = computed(() => {
  return {
    span: 22, export: true, labelWidth: 100, headerWidth: 60, searchSpan: 4, searchLabelWidth: 60,
    column: [{
      label: `${i18n.t('AGVCore.AGVBrand')}`, field: 'agvBrandCode', type: 'select', options: AGVBrands.value,
      minWidth: 80, props: { label: `${toI18nName('agvBrandNameZhCn')}`, value: 'agvBrandCode' }
    }, {
      label: `${i18n.t('AGVCore.AGVCode')}`, field: 'agvCode', minWidth: 80
    }, {
      label: `${i18n.t('AGVCore.ChargeStartTime')}`, field: 'chargeStartTime', type: 'daterange', minWidth: 90
    }, {
      label: `${i18n.t('AGVCore.ChargingPileCode')}`, field: 'chargeStation', minWidth: 90, hide: true, search: false
    }, {
      label: `${i18n.t('AGVCore.PreChargeDistance')}(M)`, field: 'beforeChargeMil', minWidth: 100, search: false,
      hide: true
    }, {
      label: `${i18n.t('AGVCore.PreChargeDuration')}(H)`, field: 'beforeChargeTime', minWidth: 100, search: false,
      hide: true
    }, {
      label: `${i18n.t('AGVCore.PreChargePower')}`, field: 'chargeStartNum', minWidth: 100, search: false,
      format: val => `${val || 0}%`
    }, {
      label: `${i18n.t('AGVCore.ChargeCurrent')}`, field: 'current', minWidth: 100, search: false, hide: true
    }, {
      label: `${i18n.t('AGVCore.ChargeDuration')}`, field: 'chargeTime', minWidth: 100, search: false
    }, {
      label: `${i18n.t('AGVCore.ChargeEndPower')}`, field: 'chargeEndNum', minWidth: 100, search: false,
      format: val => `${val || 0}%`
    }, {
      label: `${i18n.t('AGVCore.ChargeEndReason')}`, field: 'chargeEndText', options: endChargeReasons.value,
      minWidth: 100, search: false
    }, {
      label: `${i18n.t('AGVCore.DailyPowerRange')}`, field: 'DailyPowerRange', minWidth: 100, search: false,
      format: (_, row) => `${row.chargeMinNum || 0}-${row.chargeMaxNum || 0}%`
    }]
  }
})

const rowDblClick = row => {
  formInfo.value = row
  modalVisible.value = true
}
const formatSearchForm = () => {
  const params = { ...searchForm.value }
  if (searchForm.value.chargeStartTime?.from) {
    params.startTime = params.chargeStartTime?.from
    params.endTime = params.chargeStartTime?.to
    delete params.chargeStartTime
  }
  return { ...params }
}
const startSearch = (_, done) => {
  remoteData.value.params = formatSearchForm()
  remoteData.value.clearSelected = true
  if (done) done()
}
onMounted(startSearch)
</script>